import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {//存放数据的仓库
    user: null,
    showLogin: false, //登录组件是否展示
    shopcartList: []
  },
  getters: {//相当于计算属性
    userInfo(state) {
      return state.user
    },
    userName(state, getters) {
      //容错处理,刚开始未登录的时候 user是没有内容的,如果直接读userInfo则返回null
      return getters.userInfo ? getters.userInfo.userName : ""
    },
    getShowLogin(state) {
      return state.showLogin
    },

    getShopCart(state) { // 获取购物车所有的数据
      return state.shopcartList
    },
    getCkAll(state) { // 获取全选
      let flag = state.shopcartList.every(item => item.check)
      return flag
    },
    getAllNum(state) { // 获取所有的数量
      let num = 0
      state.shopcartList.forEach(item => {
        num += item.num
      })
      return num
    },
    getAllCheckGoods(state) { // 获取选中的商品
      let num = 0
      state.shopcartList.forEach(item => {
        if (item.check) {
          num += item.num
        }
      })
      return num
    },
    getAllTotal(state) { // 获取总价
      let num = 0
      state.shopcartList.forEach(item => {
        if (item.check) {
          num += item.num * item.price
        }
      })
      return num
    },
    showGoShop(state) { // 判断子复选框是否存在选中的，存在就让 去结算 显示
      let flag = state.shopcartList.some(item => item.check)
      return flag
    },
    getCheckShopList(state) { // 过滤出选中的商品
      return state.shopcartList.filter(item => item.check)
    },
    // confirmorder.vue使用
    getCheckGoods(state) {
      // 获取勾选的商品信息
      // 用于确认订单页面
      let checkGoods = [];
      for (let i = 0; i < state.shopcartList.length; i++) {
        const temp = state.shopcartList[i];
        if (temp.check) {
          checkGoods.push(temp);
        }
      }
      return checkGoods;
    },
  },
  mutations: {
    setUser(state, data) {
      state.user = data;
    },
    setShowLogin(state, data) {
      state.showLogin = data;
    },

    setShopCart(state, data) {
      state.shopcartList = data
      console.log(state.shopcartList);
    },
    setCkAll(state, bool) {
      state.shopcartList.forEach(item => item.check = bool)
    },
    setUpdateShopCartCheck(state, obj) {
      let index = state.shopcartList.findIndex(item => item.id == obj.id)
      state.shopcartList[index].check = obj.check
    },
    setUpdateShopCartNum(state, obj) {
      // console.log(obj);
      let index = state.shopcartList.findIndex(item => item.productID == obj.product_id)
      state.shopcartList[index].num = obj.num
      state.shopcartList[index].check = true
    },
    delHandle(state, id) {
      let index = state.shopcartList.findIndex(item => item.productID == id)
      state.shopcartList.splice(index, 1)
    },
    deleteShoppingCart(state, id) {
      // 根据购物车id删除购物车商品
      for (let i = 0; i < state.shopcartList.length; i++) {
        const temp = state.shopcartList[i];
        if (temp.id == id) {
          state.shopcartList.splice(i, 1);
        }
      }
    },
  },
  actions: {
    // 异步操作
    setShopCart({
      commit
    }, payload) {
      commit('setShopCart', payload)
    },
    setCkAll({
      commit
    }, bool) {
      commit('setCkAll', bool)
    },
    setUpdateShopCartCheck({
      commit
    }, payload) {
      commit('setUpdateShopCartCheck', payload)
    },
    async setUpdateShopCartNum(ctx, payload) { // 向后端发送修改num操作
      // updateShoppingCart
      // console.log(payload,ctx);
      let obj = {
        user_id: ctx.state.user.user_id,
        product_id: payload.productID,
        num: payload.num,
      }
      let {
        data: res
      } = await shoppingcartAPI.updateShoppingCart(obj)
      if (res.code === '001') {
        Notification({
          title: "成功",
          message: res.msg,
          type: "success",
        })
        ctx.commit('setUpdateShopCartNum', obj)
      }
    },
    async delHandle(ctx, id) { // 向后端发送删除操作
      // deleteShoppingCart
      let obj = {
        user_id: ctx.state.user.user_id,
        product_id: id,
      }
      let {
        data: res
      } = await shoppingcartAPI.deleteShoppingCart(obj)
      if (res.code === '001') {
        Notification({
          title: "成功",
          message: res.msg,
          type: "success",
        })
        ctx.commit('delHandle', id)
      }
    },
    async toShop(ctx) { // 向后端发起结算，结算完需要跳转订单页面
      let obj = {
        user_id: ctx.state.user.user_id,
        products: ctx.getters.getCheckShopList,
      }
      let {
        data: res
      } = await order.addOrder(obj)
      console.log(res);
    },
    // ........................................
    deleteShoppingCart({
      commit
    }, id) {
      commit('deleteShoppingCart', id);
    },
  },
  modules: {
  }
})
